package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.entity.PaySuccessEntity;

import butterknife.BindView;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/15 08:51
 */
public class WechatFundPaySucDetailActivity extends BaseActivity {

    @BindView(R2.id.tv_money)
    TextView       mTvMoney;
    @BindView(R2.id.tv_money_tag)
    TextView       mTvMoneyTag;
    @BindView(R2.id.re_shop)
    RelativeLayout mReShop;
    @BindView(R2.id.re_person)
    RelativeLayout mRePerson;
    @BindView(R2.id.tv_money_tag_shop)
    TextView       mTvMoneyTagShop;
    @BindView(R2.id.tv_money_shop)
    TextView       mTvMoneyShop;
    @BindView(R2.id.tv_receiver_shop)
    TextView       mTvReceiverShop;
    @BindView(R2.id.iv_receiver)
    ImageView      mIvReceiver;
    @BindView(R2.id.tv_receiver)
    TextView       mTvReceiver;
    @BindView(R2.id.tv_gzh)
    TextView       mTvGzh;
    @BindView(R2.id.ll_gzh)
    LinearLayout   mLlGzh;

    public WechatFundPaySucDetailActivity() {
        super(R.layout.activity_fund_pay_suc_detail);
    }

    public static void start(Activity activity, PaySuccessEntity paySuccessEntity, WechatContactBean wechatContactBean) {
        Intent it = new Intent(activity, WechatFundPaySucDetailActivity.class);
        it.putExtra("paySuccessEntity", paySuccessEntity);
        it.putExtra("wechatContactBean", wechatContactBean);
        activity.startActivity(it);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        setStatusBarColor(Color.parseColor("#ffffff"));
        setNavigationColor(Color.parseColor("#f7f7f7"));

        initTypeFace();

        PaySuccessEntity paySuccessEntity = (PaySuccessEntity) getIntent().getSerializableExtra("paySuccessEntity");
        WechatContactBean wechatContactBean = (WechatContactBean) getIntent().getSerializableExtra("wechatContactBean");

        if (paySuccessEntity != null) {
            switch (paySuccessEntity.getPayType()) {
                case 0://个人转账
                    mReShop.setVisibility(View.VISIBLE);
                    mRePerson.setVisibility(View.GONE);
                    mTvReceiverShop.setText("待" + paySuccessEntity.getName() + "确认收钱");
                    mTvMoneyShop.setText(paySuccessEntity.getMoney());
                    break;
                case 1://个人收款
                    mReShop.setVisibility(View.GONE);
                    mRePerson.setVisibility(View.VISIBLE);
                    mTvMoney.setText(paySuccessEntity.getMoney());
                    if (wechatContactBean != null) {//收款人
                        mTvReceiver.setText(getReceiverShowValue(wechatContactBean, paySuccessEntity));
                        WechatContactBean.setupAvatarIntoImageView(wechatContactBean, mIvReceiver, 12);
                    }
                    break;
                case 2://商户收款
                    mReShop.setVisibility(View.VISIBLE);
                    mRePerson.setVisibility(View.GONE);
                    mTvReceiverShop.setText(paySuccessEntity.getName());
                    mTvMoneyShop.setText(paySuccessEntity.getMoney());
                    if (paySuccessEntity.isAttention()) {
                        mLlGzh.setVisibility(View.VISIBLE);
                        mTvGzh.setText(paySuccessEntity.getGzhName());
                    } else {
                        mLlGzh.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    }

    private String getReceiverShowValue(WechatContactBean wechatContactBean, PaySuccessEntity paySuccessEntity) {
        String name = wechatContactBean.getName();
        String realName = paySuccessEntity.getName();
        if (!TextUtils.isEmpty(name) && name.contains("（我）"))
            name = name.replace("（我）", "");
        if (!TextUtils.isEmpty(realName)) {
            if (realName.length() < 2)
                realName = realName;
            else if (realName.length() < 3)
                realName = "*" + realName.substring(1);
            else
                realName = "**" + realName.substring(2);
        }
        return name + "(" + realName + ")";
    }

    private void initTypeFace() {
        AssetManager mgr = getAssets();
        Typeface tf = Typeface.createFromAsset(mgr, "fonts/WeChatLqNumber.ttf");
        mTvMoney.setTypeface(tf);
        mTvMoneyTag.setTypeface(tf);
        mTvMoneyShop.setTypeface(tf);
        mTvMoneyTagShop.setTypeface(tf);
    }
}
